﻿using System;
using System.Collections.Generic;

namespace InputBoxes.Controls
{
    public class OrderedValueTypeInputBoxBase<T> : ParsingValueTypeInputBoxBase<T>
        where T : struct, IComparable<T>
    {
        public T MinValue { get; set; }
        public T MaxValue { get; set; }

        protected override bool GetIsValid(string text)
        {
            return base.GetIsValid(text) && ValueInRange(Parse(text));
        }

        private bool ValueInRange(T? value)
        {
            if (!value.HasValue) return false;

            Comparer<T> comparer = Comparer<T>.Default;

            if (comparer.Compare(MinValue, MaxValue) == 0) return true;

            return (comparer.Compare(MinValue, value.Value) <= 0) && (comparer.Compare(value.Value, MaxValue) <= 0);
        }
    }
}